package org.stvd.service.workflow.impl;

import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.stvd.common.Consts;
import org.stvd.common.SecurityUserHolder;
import org.stvd.common.utils.ServiceResult;
import org.stvd.core.dto.QueryResult;
import org.stvd.core.util.DateUtil;
import org.stvd.core.util.IdWorker;
import org.stvd.entities.admin.Department;
import org.stvd.entities.admin.Users;
import org.stvd.entities.workflow.WfUser;
import org.stvd.repository.workflow.WfUserDao;
import org.stvd.service.admin.DepartmentService;
import org.stvd.service.admin.UsersService;
import org.stvd.service.base.impl.BaseServiceImpl;
import org.stvd.service.workflow.WfUserService;

@Service("WfUserService")
public class WfUserServiceImpl extends BaseServiceImpl<WfUser> implements WfUserService {

	@Resource(name = "WfUserDao")
	private WfUserDao wfUserDao;
	@Autowired
	private DepartmentService departmentService;
	@Autowired
	private UsersService usersService;

	@Override
	public QueryResult<Map<String, Object>> wfUserQuery(int start, int limit, String groupCode, String createUid) {
		return wfUserDao.wfUserQuery(start, limit, groupCode, createUid);
	}

	@Override
	public List<WfUser> listWfUserByCode(String groupCode) {
		return wfUserDao.listWfUserByCode(groupCode);
	}

	@Override
	public  WfUser getWfUserByUid(String uid) {
		return wfUserDao.getWfUserByUid(uid);
	}
	@Override
	@Transactional
	public ServiceResult<Map<String, Object>> insertWfUser(WfUser wfUser) {
		ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
		return serviceResult;
	}

	@Override
	@Transactional
	public ServiceResult<Map<String, Object>> insertWfUser(String ids) {
		ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
		if (ids == null || ids.length() <= 0) {
			return serviceResult;
		}
		if(ids.startsWith(",")) {
			ids=ids.substring(1);
		}
		String[] uids = ids.split(",");
		List<WfUser> wfUserList=wfUserDao.findAll(WfUser.class);
		if(wfUserList!=null) {
			for(int j=0;j<wfUserList.size();j++) {
				WfUser user=wfUserList.get(j);
				wfUserDao.delete(WfUser.class, user.getId());
			}
		}
		for (int i = 0; i < uids.length; i++) {
			Users user = usersService.findById(uids[i]);
			if (user == null) {
				serviceResult.setDataError("用户信息错误，请重试！");
				return serviceResult;
			}
			List<Department>  userDepList=departmentService.findDepByUid(uids[i], Consts.GUID);
			Department userDep=null;;
			if(userDepList!=null&&userDepList.size()>0) {
				userDep=userDepList.get(0);
				if (userDep == null) {
					serviceResult.setDataError("机构信息错误，请重试！");
					return serviceResult;
				}
			}
			
			WfUser wfUser = wfUserDao.getWfUserByUid(uids[i]);
			if(wfUser==null) {
				wfUser=new WfUser();
				wfUser.setId(IdWorker.getInstance().nextId());
				wfUser.setUserId(uids[i]);
				wfUser.setUserName(user.getUname());
				wfUser.setDepId(userDep.getDepId());
				wfUser.setDepName(userDep.getDepName());
				wfUser.setStatus("T");
				wfUser.setCreateUid(SecurityUserHolder.getCurrentUserid());
				wfUser.setCreateTime(DateUtil.getSystemDate());
				wfUser.setModifyTime(DateUtil.getSystemDate());
				wfUserDao.update(wfUser);
			}
		}

		return serviceResult;
	}

	@Override
	@Transactional
	public ServiceResult<Map<String, Object>> deleteWfUser(Long id) {
		ServiceResult<Map<String, Object>> serviceResult = new ServiceResult<Map<String, Object>>();
		wfUserDao.delete(WfUser.class, id);
		serviceResult.setMessage("删除成功！");
		return serviceResult;
	}
	
	public QueryResult<Map<String,Object>> wfUserQueryResultByName(Integer start, Integer limit, String uName, String createUid){
		return wfUserDao.wfUserQueryResultByName(start, limit, uName, createUid);
	}
}
